Learning Dart by 2014
Author:2014
Language: eng
Format: epub
Publisher: Packt Publishing
Spiral 1 – drawing the board
The app starts with main() in educ_memory_game.dart:
library memory; import 'dart:html'; import 'dart:async'; part 'board.dart'; void main() { // Get a reference to the canvas. CanvasElement canvas = querySelector('#canvas'); (1) new Board(canvas); (2) }
Note
For code files of this section refer to chapter 7\educ_memory_game\spirals\s01 in the code bundle.
As we did in Chapter 5, Handling the DOM in a New Way, we'll draw a board on a canvas element. So, we need a reference that is given in line (1). The Board view is represented in code as its own Board class in the board.dart file. Since everything happens on this board, we construct its object with canvas as argument (line (2)). Our game board is periodically drawn as a rectangle in line (4) by using the animationFrame method from the Window class in line (3):
part of memory; class Board { // The board is drawn every INTERVAL in ms. static const int INTERVAL = 8; CanvasElement canvas; CanvasRenderingContext2D context; num width, height; Board(this.canvas) { context = canvas.getContext('2d'); width = canvas.width; height = canvas.height; // Draw every INTERVAL in ms. window.animationFrame.then(gameLoop); (3) } void gameLoop(num delta) { draw(); window.animationFrame.then(gameLoop); } void draw() { clear(); border(); } void clear() { context.clearRect(0, 0, width, height); } void border() { context..rect(0, 0, width, height)..stroke(); (4) } }
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9928)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
Dependency Injection in .NET by Mark Seemann(9348)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7791)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7567)
Svelte with Test-Driven Development by Daniel Irvine(7266)
Test-Driven Development with PHP 8 by Rainier Sarabia(7002)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6859)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6541)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6424)
Web Development with Django by Ben Shaw Saurabh Badhwar(6321)
React Application Architecture for Production by Alan Alickovic(6044)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5816)
Kotlin in Action by Dmitry Jemerov(5074)
Audition by Ryu Murakami(4594)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4507)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4357)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4061)
